package com.yiren.dbaa.view.popup;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.util.DensityUtil;

/**
 * @author lwb
 * @date :2022/1/10 14:11
 * @description: 调整车辆模型的弹窗
 */
public class CarPointAdjustPopup extends PopupWindow implements View.OnClickListener {

    private Context mContext;

    private Rect mRect = new Rect();   //实例化一个矩形
    private final int[] mLocation = new int[2];  //坐标的位置（x、y）
    private int mScreenWidth, mScreenHeight;  //屏幕的宽度和高度
    private int popupGravity = Gravity.NO_GRAVITY;  //位置不在中心
    private TextView tvValue;
    private int value;   //弹框的值

    private OnViewClickListener listener;

    public CarPointAdjustPopup(Context context, int width, int height) {
        this.mContext = context;
        //设置可以获得焦点
        setFocusable(true);
        //设置弹窗内可点击
        setTouchable(true);
        //设置弹窗外可点击
        setOutsideTouchable(true);
        //获得屏幕的宽度和高度
        mScreenWidth = DensityUtil.getScreenWidth(mContext);
        mScreenHeight = DensityUtil.getScreenHeight(mContext);
        //设置弹窗的宽度和高度
        setWidth(DensityUtil.dip2px(mContext, width));
        setHeight(DensityUtil.dip2px(mContext, height));

        setBackgroundDrawable(new BitmapDrawable());

        //设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.popup_car_adjust, null));
        initPopView();
    }

    /**
     * 初始化弹窗列表.
     */
    private void initPopView() {
        View contentView = getContentView();
        tvValue = (TextView) contentView.findViewById(R.id.tv_value);
        ((ImageView) contentView.findViewById(R.id.iv_up)).setOnClickListener(this);
        ((ImageView) contentView.findViewById(R.id.iv_left)).setOnClickListener(this);
        ((ImageView) contentView.findViewById(R.id.iv_right)).setOnClickListener(this);
        ((ImageView) contentView.findViewById(R.id.iv_bottom)).setOnClickListener(this);
        ((TextView) contentView.findViewById(R.id.tv_reduce)).setOnClickListener(this);
        ((TextView) contentView.findViewById(R.id.tv_add)).setOnClickListener(this);
    }

    /**
     * 显示弹窗列表界面.
     *
     * @param view 点击的控件
     */
    public void show(View view) {
        tvValue.setText("1cm");
        value = 1;
        //获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);
        //设置矩形的大小
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(), mLocation[1] + view.getHeight());
        //显示弹窗的位置
        showAtLocation(view, popupGravity, mLocation[0] - getWidth(), (mRect.top + mRect.bottom - getHeight()) / 2);
    }

    public void setOnViewClickListener(OnViewClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_up:  //上
                listener.onItemClick(1, value / 100.0f);
                break;
            case R.id.iv_left:  //左
                listener.onItemClick(2, value / 100.0f);
                break;
            case R.id.iv_right:  //右
                listener.onItemClick(3, value / 100.0f);
                break;
            case R.id.iv_bottom:  //下
                listener.onItemClick(4, value / 100.0f);
                break;
            case R.id.tv_reduce:  //减
                if (value <= 1) {
                    return;
                } else {
                    value--;
                    tvValue.setText(value + "cm");
                }
                break;
            case R.id.tv_add:  //加
                if (value > 50) {
                    return;
                } else {
                    value++;
                    tvValue.setText(value + "cm");
                }
                break;
        }
    }

    public interface OnViewClickListener {

        /**
         * @param flag 1:上 2：左 3：右 4：下
         */
        void onItemClick(int flag, float value);
    }

}
